package hc.bre.clas

import static org.junit.jupiter.api.Assertions.*

import org.junit.jupiter.api.Test
import hc.bre.clas.range.*
import hc.bre.runner.ScriptLoaderRunner

@groovy.transform.CompileStatic
class RangeTest {
	String jsonText =
	'''
{
    "list": [
		{
            "range": "其它",
            "id": 999,
            "code": "未知",
			"otherflag":true
        },
        {
            "range": "1",
            "id": 1,
            "code": "女性"
        },
        {
            "range": "2",
            "id": 2,
            "code": "男性"
        }
    ],
    "rname": "性别分类",
    "rversion": 8,
    "rmemo": "这是一个数字分段示例",
    "rpath": "xsxdjc/class9/性别分类",
    "rid": 16,
    "rtype": "rule:clas:range",
    "variableId": 0
}
'''

	@Test
	void create() {
		RangeDefinition ruleDef = RangeDefinition.create();
		assert ruleDef.list[0].otherflag == 1
		assert ruleDef.list.size() == 1
	}

	@Test
	void load() {
		RangeDefinition ruleDef = new RangeDefinition()
		ruleDef.load(hc.Util.jsonRead(jsonText));

		assert ruleDef.list.size() == 3
		assert ruleDef.list[0].id ==999
	}


	@Test
	@groovy.transform.CompileDynamic
	void buildAndEvalute() {
		RangeDefinition ruleDef = new RangeDefinition().load(jsonText);

		RangeBuilder builder = new RangeBuilder() {
					@Override
					public String rangeVariable() {
						'性别'
					}
				};

		ScriptLoaderRunner runner = ScriptLoaderRunner.forRule(ruleDef,builder);

		def ret = runner.run(性别:1);
		assert ret['result'].clas.id == 1
		assert ret['result'].clas.code == '女性'

		ret = runner.run(性别:2);
		assert ret['result'].clas.id == 2
		assert ret['result'].clas.code == '男性'

		ret = runner.run(性别:11);
		assert ret['result'].clas.id == 999
		assert ret['result'].clas.code == '未知'

	}
}
